import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:get/get.dart';

class RelatedTicketSelectionController extends GetxController {
  bool? isRadio;
  List<ExecuteTicketReceiptList> selectedList = []; // 选中的列表

  @override
  void onInit() {
    super.onInit();
    final Map<String, dynamic> arguments =
        Get.arguments as Map<String, dynamic>;
    //获取是否单选
    isRadio = arguments['isRadio'] as bool;
  }

  /// 查询作业中小票列表
  Future<List<ExecuteTicketReceiptList>> getDate(int pageIndex, int pageSize,
      {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<ExecuteTicketReceiptList>>(
        Interface.getListReceipt,
        queryParameters: {
          'receiptNo': search,
          'pageNum': pageIndex,
          'pageSize': pageSize
        }).execute();

    List<ExecuteTicketReceiptList>? data;
    res.onSuccess((result) {
      data = result.data;
    });
    return data ?? [];
  }

  /// 选择点击事件
  void onTap(ExecuteTicketReceiptList item) {
    //如果是单选
    if (isRadio ?? true) {
      for (var selectItem in selectedList) {
        //清空之前选中的数据
        selectItem.setIsChecked(false);
      }
      item.setIsChecked(true);
      selectedList.clear();
      selectedList.add(item);
    } else {
      //判断点击前的状态
      final bool isChecked = item.getIsChecked();
      if (isChecked) {
        //如果之前是选中的，就修改状态后移出
        item.setIsChecked(false);
        selectedList.remove(item);
      } else {
        //如果之前是未选中的，就修改状态后添加
        item.setIsChecked(true);
        selectedList.add(item);
      }
    }
    update();
  }

  ///完成
  void complete() {
    Get.back(result: selectedList);
  }
}
